<?php

function report_karyawan(){
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'nik'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'nama'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'tmb'=>array('style'=>'border-bottom:2px solid;width:15%'), 		
		'jabatan'=>array('style'=>'border-bottom:2px solid;width:30%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	if(isset($_GET['key']))
	$query = "SELECT * FROM   karyawan c  
		INNER JOIN biodata d ON d.biodata_id = c.biodata_id
		WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		AND c.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 
		AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' )
		";
	else
	$query = "
		SELECT * FROM karyawan a 
		WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 		
	";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =15;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 15; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		$jabatan =  loaddata_jabatan($karyawan['jabatan_id']);  
		
		$detailproperty = array(
			'href'=>'index.php?com=pr_master_gaji&task=detail&id='.$ey['karyawan_id'],
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),
			'nik'=>position_text_align( $karyawan['nik'], 'center'),
			'nama'=> $karyawan['nama_gelar'],  
			'tmb'=>date( 'd - m - Y' , strtotime($karyawan['tmb']) ),  
			'jabatan'=> $jabatan['nama_jabatan'],  
			'operasi'=> position_text_align( $detail_button .' '.$edit_button, 'right'),  
		);
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&key='.$_GET['key'].'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel&key='.$_GET['key'].'\';'),
	);	
	
	$printmenu = additional_menu_on_list( $menuontop, $search);
	return $printmenu .table_builder($header , $datas ,  15 ,false , $paging  );
} 

function form_detail($id){

	$view  = form_header( "detail jabatan" , "cp"  );
	$view = '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value=" BACK " class="main_button" onclick="javascript:location.href=\'index.php?com=pr_master_gaji\'"/>
	</div></td></tr>'   ; 	
	$view .= form_footer( ); 
//	$pro  = prokaryawan($id);
	$karyawan = loaddata_karyawan($id);
	$view =   karyawan_header_detail($karyawan, true  );
	$karyawan_status_id = $karyawan['status_kekaryawan_id'];
	$st = array(
		'1'=>'Organik',
		'2'=>'Kontrak',
		'3'=>'Perbantuan',
		'4'=>'Alih_Status',
	);
	$status = strtolower($st[$karyawan_status_id]);
	$status = (trim($status) <> '') ? $status : $st[1];
	//CEK PERAWAT
	if($karyawan['profesi_detail_id']==0){
		$query_perawat=" AND model_entry<> 4 ";
	}else{
		$query_perawat="" ;
	}	
	
//	GAJI	
	$header = array(
		'No.'=>array('style'=>'text-align:left;border-bottom:2px solid;width:5%'),  
		'Keterangan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:60%'),  
		'Nominal (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:35%'),  
	);
	$row = array();
	$row[] = array(
			'no'=>1 ,
			'nama'=>'Gaji KM' ,
			'nominal'=>position_text_align(rp_format($karyawan['gapok_km']) ,'right'),
		);
	$row[] = array(
			'no'=>2 ,
			'nama'=>'Gaji KS' ,
			'nominal'=>position_text_align(rp_format( $karyawan['gapok_ks']) ,'right'),
		);
	$row[] = array(
			'no'=>3 ,
			'nama'=>'PhDP PPMP' ,
			'nominal'=>position_text_align(rp_format( $karyawan['PPMP'] ) ,'right'),
		);
	$row[] = array(
			'no'=>4 ,
			'nama'=>'PhDP PPIP' ,
			'nominal'=>position_text_align(rp_format( $karyawan['PPIP'] ) ,'right'),
		);

		
	$datas = table_rows_plain($row);	
	$gaji_table = table_builder_plain($header , $datas , 4, false );	
	
	
//	TUNJANGAN	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tunjangan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:75%'),  
		'Nominal (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
	);

	
	$query = "SELECT nama_komponen_gaji, komponen_gaji_id
		FROM pr_komponen_gaji 
		WHERE remunerasi='Y'  
		AND tipe=1 
		AND (model_entry<>2 OR
		( model_entry=2 AND ".$status."='yes'))".$query_perawat;
	$result = my_query($query);	
	$row = array();
	$i=0;
	$total=0 ;

	while($ey = my_fetch_array($result)){
		$nominal=kalkulasi_komponen($ey['komponen_gaji_id'] , $id);
		if($nominal>0){
			$i++;
			$row[] = array(
				'e'=>position_text_align($i,'center'),
				'nama'=>$ey['nama_komponen_gaji'] ,
				'nom'=> position_text_align(rp_format(
					$nominal ), 'right' ),
			);
			$total+=$nominal;
		}
	}
	$row[] = array(
		'e'=>' ',
		'nama'=>' T O T A L' ,
		'nom'=> position_text_align(rp_format(
			$total ), 'right' ),
	);
	if($i < 5){
		for($ycc=$i; $ycc<=4; $ycc++){
			$row[] = array(
				'#'=> '&nbsp;',  
				'nama'=>  '&nbsp;', 
				'nom'=>  '&nbsp;',  
			); 
		}
	}
	
	$datas = table_rows_plain($row);	
	$tunj_table = table_builder_plain($header , $datas , 4, false );

//	POTONGAN	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Potongan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:65%'),  
		'Nominal (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:30%'),  
	);
	$query = "SELECT nama_komponen_gaji, komponen_gaji_id
		FROM pr_komponen_gaji 
		WHERE remunerasi='Y'  
		AND tipe=2 
		AND (model_entry<>2 OR
		( model_entry=2 AND ".$status."='yes'))".$query_perawat;
	
	$result = my_query($query);	
	$row = array();
	$i=0;
	$total=0 ;

	while($ey = my_fetch_array($result)){
		$nominal=kalkulasi_komponen($ey['komponen_gaji_id'] , $id);
		if($nominal>0){
			$i++;
			$row[] = array(
				'e'=>position_text_align($i,'center'),
				'nama'=>$ey['nama_komponen_gaji'] ,
				'nom'=> position_text_align(rp_format(
					$nominal ), 'right' ),
			);
			$total+=$nominal;
		}
	}
	$row[] = array(
		'e'=>' ',
		'nama'=>' T O T A L' ,
		'nom'=> position_text_align(rp_format(
			$total ), 'right' ),
	);
	if($i < 5){
		for($ycc=$i; $ycc<=4; $ycc++){
			$row[] = array(
				'#'=> '&nbsp;',  
				'nama'=>  '&nbsp;', 
				'nom'=>  '&nbsp;',  
			); 
		}
	}
	
	$datas = table_rows_plain($row);	
	$pot_table = table_builder_plain($header , $datas , 4, false );

	
//	IURAN	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Iuran'=>array('style'=>'text-align:left;border-bottom:2px solid;width:65%'),  
		'Nominal (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:30%'),  
	);

	$query = "SELECT nama_komponen_gaji, komponen_gaji_id
		FROM pr_komponen_gaji 
		WHERE remunerasi='Y'  
		AND tipe=3 
		AND (model_entry<>2 OR
		( model_entry=2 AND ".$status."='yes'))".$query_perawat;

	$result = my_query($query);	
	$row = array();
	$i=0;
	$total=0 ;

	while($ey = my_fetch_array($result)){
		$nominal=kalkulasi_komponen($ey['komponen_gaji_id'] , $id);
		if($nominal>0){
			$i++;
			$row[] = array(
				'e'=>position_text_align($i,'center'),
				'nama'=>$ey['nama_komponen_gaji'] ,
				'nom'=> position_text_align(rp_format(
					$nominal ), 'right' ),
			);
			$total+=$nominal;
		}
	}
	$row[] = array(
		'e'=>' ',
		'nama'=>' T O T A L' ,
		'nom'=> position_text_align(rp_format(
			$total ), 'right' ),
	);
	if($i < 5){
		for($ycc=$i; $ycc<=4; $ycc++){
			$row[] = array(
				'#'=> '&nbsp;',  
				'nama'=>  '&nbsp;', 
				'nom'=>  '&nbsp;',  
			); 
		}
	}
	
	$datas = table_rows_plain($row);	
	$iur_table = table_builder_plain($header , $datas , 4, false );
	
	$cccc = '????';
	
	$tab_options = array(
		'Gaji'=>'tab1',
		'Tunjangan'=>'tab2',
		'Potongan'=>'tab3',
		'Iuran'=>'tab4',

	);
	$contents = array('tab1'=>$gaji_table ,'tab2'=>$tunj_table,'tab3'=>$pot_table,'tab4'=>$iur_table );
	$tabcontents = tab_page($tab_options , $contents );
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak_det&id='.$id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel_det&id='.$id.'\';'),
		'Kembali'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'\';'),
	);	
	$printmenu = additional_menu_on_list( $menuontop);	
	return $pro.$view.$printmenu.$tabcontents ;
	
}

function excel_report(){
	my_component_load('xl_builder' , false);
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'nik'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'nama'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'tmb'=>array('style'=>'border-bottom:2px solid;width:15%'), 		
		'jabatan'=>array('style'=>'border-bottom:2px solid;width:30%'), 
		'gaji'=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	 
		$query = "
			SELECT * FROM karyawan a 
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 		
		";
 	$result = my_query($query ); 

	$row = array(); 
	$i = 0; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		$jabatans =  loaddata_jabatan($karyawan['jabatan_id']);  

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),
			'nik'=> '\''.sprintf('%07d',$karyawan['nik']), 
			'nama'=> $karyawan['nama_gelar'],  
			'tmb'=>date( 'd - m - Y' , strtotime($karyawan['tmb']) ),  
			'jabatan'=> $jabatans['nama_jabatan'],  
			'gaji'=> position_text_align( $karyawan['gapok_km'], 'right'),  
		);
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 15 ,false ); 
}

function print_plain(){
	if(isset($_GET['key']))
		$query = "SELECT * FROM   karyawan c  
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
			AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' )
			";
	else
		$query = "
			SELECT * FROM karyawan a 
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 		
		";
	$result = my_query($query ); 
	$datas = array();  
	$i=1;

	$datas[] = array(
		 'k1'=>array("NO" ,5 ,STR_PAD_BOTH),
		 'k2'=>array("NIK" ,10,STR_PAD_RIGHT),
		 'k3'=>array("Nama" ,30,STR_PAD_RIGHT),
		 'k4'=>array("TMB" ,15,STR_PAD_RIGHT),  
		 'k5'=>array("Jabatan" ,30,STR_PAD_RIGHT),  
		 'k6'=>array("Gaji" ,10,STR_PAD_LEFT),  
	); 
	$datas[] = array(
		 'k1'=>array(" " ,5 ,STR_PAD_BOTH),
		 'k2'=>array(" " ,10,STR_PAD_RIGHT),
		 'k3'=>array(" " ,30,STR_PAD_RIGHT),
		 'k4'=>array(" " ,15,STR_PAD_RIGHT),  
		 'k5'=>array(" " ,30,STR_PAD_RIGHT),  
		 'k6'=>array(" " ,10,STR_PAD_LEFT),  
	); 
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		$jabatans =  loaddata_jabatan($karyawan['jabatan_id']);  

		$datas[] = array(		 
			'k1'=>array($i ,5 ,STR_PAD_BOTH),
			'k2'=>array($karyawan['nik'],10,STR_PAD_RIGHT),
			'k3'=>array($karyawan['nama_gelar'] ,30,STR_PAD_RIGHT),
			'k4'=>array(date( 'd - m - Y' , strtotime($karyawan['tmb']) ),15,STR_PAD_RIGHT),  
			'k5'=>array(substr($jabatans['nama_jabatan'],0,30),30,STR_PAD_RIGHT),  
			'k6'=>array(rp_format($karyawan['gapok_km']) ,10,STR_PAD_LEFT),  
		);
	}
	$view = 	str_pad("CETAK MASTER GAJI",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}


function excel_report_det($id){
	my_component_load('xl_builder' , false);
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nama Komponen'=>array('style'=>'text-align:left;border-bottom:2px solid;width:70%'),  
		'Tipe'=>array('style'=>'text-align:left;border-bottom:2px solid;width:5%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
	);	
	$karyawan = loaddata_karyawan($id);
	$karyawan_status_id = $karyawan['status_kekaryawan_id'];
	$st = array(
		'1'=>'Organik',
		'2'=>'Kontrak',
		'3'=>'Perbantuan',
		'4'=>'Alih_Status',
	);
	$status = strtolower($st[$karyawan_status_id]);
	$status = (trim($status) <> '') ? $status : $st[1];
	//CEK PERAWAT
	if($karyawan['profesi_detail_id']==0){
		$query_perawat=" AND model_entry<> 4 ";
	}else{
		$query_perawat="" ;
	}	
	$query = "SELECT nama_komponen_gaji, tipe, komponen_gaji_id
		FROM pr_komponen_gaji 
		WHERE remunerasi='Y'  
		AND (model_entry<>2 OR
		( model_entry=2 AND ".$status."='yes'))".$query_perawat;
	$query .=" ORDER BY tipe, nama_komponen_gaji";	
	$result = my_query($query );
	$tipes = array(
		'1'=>'Tunjangan',
		'2'=>'Potongan',
		'3'=>'Iuran',
	);	

	$row = array(); 
	$row[] = array(
		'nno'=>1 ,
		'nama'=>'Gaji KM' ,
		'tipe'=>' ',
		'tot'=> position_text_align($karyawan['gapok_km'], 'right' ),
		);
	$row[] = array(
		'nno'=>2 ,
		'nama'=>'Gaji KS' ,
		'tipe'=>' ',
		'tot'=> position_text_align($karyawan['gapok_ks'], 'right' ),
		);
	$row[] = array(
		'nno'=>3 ,
		'nama'=>'PhDP PPMP' ,
		'tipe'=>' ',
		'tot'=> position_text_align($karyawan['PPMP'], 'right' ),
		);	
	$row[] = array(
		'nno'=>4 ,
		'nama'=>'PhDP PPIP' ,
		'tipe'=>' ',
		'tot'=> position_text_align($karyawan['PPIP'], 'right' ),
		);
	$i = 4; 		
	while($ey = my_fetch_array($result )){
		$nominal=kalkulasi_komponen($ey['komponen_gaji_id'] , $id);
		if($nominal>0){
			$i++;
			$row[] = array(
				'nno'=> $i,
				'nama'=>$ey['nama_komponen_gaji'] ,
				'tipe'=>$tipes[$ey['tipe']],
				'tot'=> position_text_align($nominal, 'right' ),
			); 
		}
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 15 ,false ); 
}

function print_plain_det($id){
	$karyawan = loaddata_karyawan($id);
	$karyawan_status_id = $karyawan['status_kekaryawan_id'];
	$st = array(
		'1'=>'Organik',
		'2'=>'Kontrak',
		'3'=>'Perbantuan',
		'4'=>'Alih_Status',
	);
	$status = strtolower($st[$karyawan_status_id]);
	$status = (trim($status) <> '') ? $status : $st[1];
	//CEK PERAWAT
	if($karyawan['profesi_detail_id']==0){
		$query_perawat=" AND model_entry<> 4 ";
	}else{
		$query_perawat="" ;
	}	
	$query = "SELECT nama_komponen_gaji, tipe, komponen_gaji_id
		FROM pr_komponen_gaji 
		WHERE remunerasi='Y'  
		AND (model_entry<>2 OR
		( model_entry=2 AND ".$status."='yes'))".$query_perawat;
	$query .=" ORDER BY tipe, nama_komponen_gaji";	
	$result = my_query($query );
	$datas=array();
	$datas[] = array(
		 'k1'=>array("#" ,5 ,STR_PAD_BOTH),
		 'k2'=>array("Nama Komponen" ,65,STR_PAD_RIGHT),
		 'k3'=>array("Tipe" ,10,STR_PAD_RIGHT),
		 'k4'=>array("Nominal" ,20,STR_PAD_LEFT),  
	); 
	$datas[] = array(
		 'k1'=>array(" " ,5 ,STR_PAD_BOTH),
		 'k2'=>array(" " ,65,STR_PAD_RIGHT),
		 'k3'=>array(" " ,10,STR_PAD_RIGHT),
		 'k4'=>array(" " ,20,STR_PAD_LEFT),  
	); 
	$datas[] = array(
		 'k1'=>array(1 ,5 ,STR_PAD_BOTH),
		 'k2'=>array('Gaji KM' ,65,STR_PAD_RIGHT),
		 'k3'=>array(" " ,10,STR_PAD_RIGHT),
		 'k4'=>array(rp_format($karyawan['gapok_km']) ,20,STR_PAD_LEFT),  
	); 	
	$datas[] = array(
		 'k1'=>array(2 ,5 ,STR_PAD_BOTH),
		 'k2'=>array('Gaji KS' ,65,STR_PAD_RIGHT),
		 'k3'=>array(" " ,10,STR_PAD_RIGHT),
		 'k4'=>array(rp_format($karyawan['gapok_ks']) ,20,STR_PAD_LEFT),  
	); 	
	$datas[] = array(
		 'k1'=>array(3 ,5 ,STR_PAD_BOTH),
		 'k2'=>array('PhDP PPMP' ,65,STR_PAD_RIGHT),
		 'k3'=>array(" " ,10,STR_PAD_RIGHT),
		 'k4'=>array(rp_format($karyawan['PPMP']) ,20,STR_PAD_LEFT),  
	); 	
	$datas[] = array(
		 'k1'=>array(4 ,5 ,STR_PAD_BOTH),
		 'k2'=>array('PhDP PPIP' ,65,STR_PAD_RIGHT),
		 'k3'=>array(" " ,10,STR_PAD_RIGHT),
		 'k4'=>array(rp_format($karyawan['PPIP']) ,20,STR_PAD_LEFT),  
	); 	
	$tipes = array(
		'1'=>'Tunjangan',
		'2'=>'Potongan',
		'3'=>'Iuran',
	);	
	$i=4;
	while($ey = my_fetch_array($result)){
		$nominal=kalkulasi_komponen($ey['komponen_gaji_id'] , $id);
		if($nominal>0){
			$i++;
			$datas[] = array(
				 'k1'=>array($i ,5 ,STR_PAD_BOTH),
				 'k2'=>array($ey['nama_komponen_gaji'] ,65,STR_PAD_RIGHT),
				 'k3'=>array($tipes[$ey['tipe']] ,10,STR_PAD_RIGHT),
				 'k4'=>array(rp_format($nominal) ,20,STR_PAD_LEFT),  
			); 	
		}	
	}
	$view = 	str_pad("CETAK MASTER GAJI DETAIL",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}
